using System;
using EPiServer.Common;
using EPiServer.Common.Queries;
using EPiServer.Common.Security;
using EPiServer.Community.StarViral;
using EPiServer.Community.StarViral.Queries;

namespace EPiAbstractions.Community.StarViral
{
    public class StarViralHandlerFacade : IStarViralHandlerFacade
    {
        private static StarViralHandlerFacade _instance;

        public StarViralHandlerFacade()
        {
            StarViralHandler.ReferralAdded += OnReferralAdded;
            StarViralHandler.ReferralUpdated += OnReferralUpdated;
            StarViralHandler.ReferralRemoved += OnReferralRemoved;
            StarViralHandler.CampaignAdded += OnCampaignAdded;
            StarViralHandler.CampaignUpdated += OnCampaignUpdated;
            StarViralHandler.CampaignRemoved += OnCampaignRemoved;
        }

        public static StarViralHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new StarViralHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IStarViralHandlerFacade Members

        public event EPiServerCommonEventHandler ReferralAdded;
        public event EPiServerCommonEventHandler ReferralUpdated;
        public event EPiServerCommonEventHandler ReferralRemoved;
        public event EPiServerCommonEventHandler CampaignAdded;
        public event EPiServerCommonEventHandler CampaignUpdated;
        public event EPiServerCommonEventHandler CampaignRemoved;

        public virtual Referral AddReferral(Referral referral)
        {
            return StarViralHandler.AddReferral(referral);
        }

        public virtual void UpdateReferral(Referral referral)
        {
            StarViralHandler.UpdateReferral(referral);
        }

        public virtual void RemoveReferral(Referral referral)
        {
            StarViralHandler.RemoveReferral(referral);
        }

        public virtual ReferralCollection GetReferrals(Int32 page, Int32 pageSize, ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(page, pageSize, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(Int32 page, Int32 pageSize, out Int32 totalItems, ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(Campaign campaign, Int32 page, Int32 pageSize, ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(campaign, page, pageSize, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(Campaign campaign, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                       ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(campaign, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(IUser referrer, Int32 page, Int32 pageSize, ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(referrer, page, pageSize, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(IUser referrer, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                       ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(referrer, page, pageSize, out totalItems, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(IUser referrer, Campaign campaign, Int32 page, Int32 pageSize,
                                                       ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(referrer, campaign, page, pageSize, sortOrder);
        }

        public virtual ReferralCollection GetReferrals(IUser referrer, Campaign campaign, Int32 page, Int32 pageSize, out Int32 totalItems,
                                                       ReferralSortOrder[] sortOrder)
        {
            return StarViralHandler.GetReferrals(referrer, campaign, page, pageSize, out totalItems, sortOrder);
        }

        public virtual Referral GetReferral(Int32 id)
        {
            return StarViralHandler.GetReferral(id);
        }

        public virtual Referral GetReferral(Guid guid)
        {
            return StarViralHandler.GetReferral(guid);
        }

        public virtual Referral GetReferral(String referredEmail)
        {
            return StarViralHandler.GetReferral(referredEmail);
        }

        public virtual Int32 GetNumberOfReferrals(Boolean returnRegistered)
        {
            return StarViralHandler.GetNumberOfReferrals(returnRegistered);
        }

        public virtual Int32 GetNumberOfReferrals(IUser user, Boolean returnRegistered)
        {
            return StarViralHandler.GetNumberOfReferrals(user, returnRegistered);
        }

        public virtual UserCollection GetReferrers()
        {
            return StarViralHandler.GetReferrers();
        }

        public virtual UserCollection GetReferrers(DateTime fromDate, DateTime toDate)
        {
            return StarViralHandler.GetReferrers(fromDate, toDate);
        }

        public virtual Campaign AddCampaign(Campaign campaign)
        {
            return StarViralHandler.AddCampaign(campaign);
        }

        public virtual void UpdateCampaign(Campaign campaign)
        {
            StarViralHandler.UpdateCampaign(campaign);
        }

        public virtual void RemoveCampaign(Campaign campaign)
        {
            StarViralHandler.RemoveCampaign(campaign);
        }

        public virtual Campaign GetCampaign(Int32 id)
        {
            return StarViralHandler.GetCampaign(id);
        }

        public virtual CampaignCollection GetCampaigns(SiteCollection sites, NullValueAction siteNullAction, Int32 page, Int32 pageSize,
                                                       CampaignSortOrder[] sortOrder)
        {
            return StarViralHandler.GetCampaigns(sites, siteNullAction, page, pageSize, sortOrder);
        }

        public virtual CampaignCollection GetCampaigns(SiteCollection sites, NullValueAction siteNullAction, Int32 page, Int32 pageSize,
                                                       out Int32 totalItems, CampaignSortOrder[] sortOrder)
        {
            return StarViralHandler.GetCampaigns(sites, siteNullAction, page, pageSize, out totalItems, sortOrder);
        }

        public virtual CampaignCollection GetQueryResult(CampaignQuery query)
        {
            return StarViralHandler.GetQueryResult(query);
        }

        public virtual CampaignCollection GetQueryResult(CampaignQuery query, Int32 page, Int32 pageSize)
        {
            return StarViralHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual CampaignCollection GetQueryResult(CampaignQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return StarViralHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        public virtual ReferralCollection GetQueryResult(ReferralQuery query)
        {
            return StarViralHandler.GetQueryResult(query);
        }

        public virtual ReferralCollection GetQueryResult(ReferralQuery query, Int32 page, Int32 pageSize)
        {
            return StarViralHandler.GetQueryResult(query, page, pageSize);
        }

        public virtual ReferralCollection GetQueryResult(ReferralQuery query, Int32 page, Int32 pageSize, out Int32 totalItems)
        {
            return StarViralHandler.GetQueryResult(query, page, pageSize, out totalItems);
        }

        #endregion

        public virtual void OnReferralAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (ReferralAdded != null)
                ReferralAdded(sender, e);
        }

        public virtual void OnReferralUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (ReferralUpdated != null)
                ReferralUpdated(sender, e);
        }

        public virtual void OnReferralRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (ReferralRemoved != null)
                ReferralRemoved(sender, e);
        }

        public virtual void OnCampaignAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (CampaignAdded != null)
                CampaignAdded(sender, e);
        }

        public virtual void OnCampaignUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (CampaignUpdated != null)
                CampaignUpdated(sender, e);
        }

        public virtual void OnCampaignRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (CampaignRemoved != null)
                CampaignRemoved(sender, e);
        }
    }
}